<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item name="searchKey" label="名称关键词">
						<a-input v-model:value.trim="searchFormState.searchKey" placeholder="请输入角色名称关键词"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-button type="primary" @click="table.refresh(true)">
						<template #icon>
							<SearchOutlined/>
						</template>
						查询
					</a-button>
					<a-button class="snowy-buttom-left" @click="() => searchFormRef.resetFields()">
						<template #icon>
							<redo-outlined/>
						</template>
						重置
					</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:expand-row-by-click="true"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:row-selection="options.rowSelection"
			@resizeColumn="handleResizeColumn"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="form.onOpen()">
						<template #icon>
							<plus-outlined/>
						</template>
						新增角色
					</a-button>
					<a-button danger @click="deleteBatchRole()">删除</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'category'">
					{{ $TOOL.dictTypeData('ROLE_CATEGORY', record.category) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<template v-if="record.code !='superAdmin' && record.code !='cabAdmin' && record.code !='sysAdmin'">
						<a @click="form.onOpen(record)">编辑</a>
						<a-divider type="vertical"/>
						<a-popconfirm title="确定删除此角色？" @confirm="removeOrg(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
						<a-divider type="vertical"/>
					</template>
					<a-dropdown>
						<a class="ant-dropdown-link">
							授权
							<DownOutlined/>
						</a>
						<template #overlay>
							<a-menu>
								<a-menu-item>
									<a @click="GrantResourceForm.onOpen(record)">授权资源</a>
								</a-menu-item>
								<a-menu-item>
									<a @click="openRoleUserSelector(record)">授权用户</a>
								</a-menu-item>
							</a-menu>
						</template>
					</a-dropdown>
				</template>
			</template>
			<!-- <template #summary>
				<a-table-summary-row>
					<a-table-summary-cell :index="0">汇 总</a-table-summary-cell>
					<a-table-summary-cell :index="1"></a-table-summary-cell>
					<a-table-summary-cell :index="2"></a-table-summary-cell>
					<a-table-summary-cell :index="3">
						<a-typography-text type="danger">{{ summaryData.sortCode }}</a-typography-text>
					</a-table-summary-cell>
					<a-table-summary-cell :index="4"></a-table-summary-cell>
				</a-table-summary-row>
			</template> -->
		</s-table>
	</a-card>


	<grantResourceForm ref="GrantResourceForm" @successful="table.refresh(true)"/>
	<Form ref="form" @successful="table.refresh(true)"/>
	<user-selector-plus
		ref="userselectorPlusRef"
		page-url="/sys/role/userSelector"
		org-url="/sys/role/orgTreeSelector"
		@onBack="userCallBack"
	/>
</template>

<script setup name="bizRole">
	import {message, Empty} from 'ant-design-vue'
	import roleApi from '@/api/sys/roleApi'
	import grantResourceForm from './grantResourceForm.vue'
	import userSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
	import Form from './form.vue'

	const columns = [
		{
			title: '编号',
			dataIndex: 'code',
			resizable: true,
			width: 150
		},
		{
			title: '角色名称',
			dataIndex: 'name',
			resizable: true,
			width: 150
		},
		{
			title: '角色描述',
			dataIndex: 'roleDesc'
		},
		{
			title: '角色人数',
			dataIndex: 'roleNum'
		},
		{
			title: '排序',
			dataIndex: 'sortCode'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '200px'
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	// 定义tableDOM
	const table = ref()
	const form = ref()
	const GrantResourceForm = ref()
	const userselectorPlusRef = ref()
	const searchFormRef = ref()
	let searchFormState = reactive({})
	// 记录数据
	const recordCacheData = ref({})

	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		let param = Object.assign(parameter, searchFormState)
		return roleApi.rolePage(param).then((res) => {
			return res
		})
	}
	// 可伸缩列
	const handleResizeColumn = (w, col) => {
		col.width = w
	}
	// 删除
	const removeOrg = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		roleApi.roleDelete(params).then(() => {
			message.success('删除成功')
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchRole = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		roleApi.roleDelete(params).then(() => {
			message.success('删除成功')
			table.value.clearRefreshSelected()
		})
	}
	// 打开用户选择器
	const openRoleUserSelector = (record) => {
		// 打开人员选择器的时候，缓存一个记录数据
		recordCacheData.value = record
		// 查询接口，查到这个角色是多少个用户都有它
		const param = {
			id: record.id
		}
		roleApi.roleOwnUser(param).then((data) => {
			userselectorPlusRef.value.showUserPlusModal(data)
		})
	}
	// 人员选择器回调
	const userCallBack = (value) => {
		const param = {
			id: recordCacheData.value.id,
			grantInfoList: value.map((item) => {
				return item.id
			})
		}
		roleApi.roleGrantUser(param).then(() => {
			message.success('保存成功')
			table.value.refresh(true)
		})
	}
</script>
